import { CLOSE,VIEW_DIR, DOWNLOADFILE, MAXSIZEWIN, MINSIZEWIN, RENDERPROCESSCALL, SHOWDIALOG, CHECK_FILE_EXIST, OPEN_CALL_WINDOW, CALL_WINDOW_CLOSE_REPLY, CLOSE_CALL_WINDOW, SELECT_FILES, GET_VIDEO_INFO,VIDEO_FFPEG,TERMINATE_VIDEO_CONVERSION,AUDIO_FFPEG,SELECT_DIR } 
from "./const";

const {ipcRenderer, remote  } = require('electron');
// import { ipcRenderer, remote } from 'electron';
import { v4 as uuidv4 } from 'uuid';
const dialog = remote.dialog;
const WIN = remote.getCurrentWindow();
const SUPPORT_IMAGE_TYPE = ['png', 'jpg', 'gif', 'PNG', 'JPG', 'GIF', 'jpeg'];
const SUPPORT_VIDEO_TYPE = ['MP4', 'MOV', 'mp4', 'm4v','avi','wmv','rmvb','3gb','mkv','flv'];
const SUPPORT_AUDIO_TYPE = ['MP3', 'm4a', 'mav', 'flac','OGG','aac','amr','wma'];
const isWin = () => {
    const platform = process.platform;
    return platform === 'win32' || platform === 'linux';
}
const minSizeWin = () => {
    WIN.minimize();
}

const maxSizeWin = () => {
    if (WIN.isMaximized()) {
        WIN.unmaximize()
    } else {
        WIN.maximize();
       
    }
}
const closeWin = () => {
    WIN.close()
}

const showDialog = () => {
    ipcRenderer.send(RENDERPROCESSCALL, {
        type: SHOWDIALOG
    })
}
const openPath = (path) => {
    ipcRenderer.send(RENDERPROCESSCALL, {
        type: VIEW_DIR,
		params: path
    })
}
const downloadFilesByUrl = (url) => {
    ipcRenderer.send(RENDERPROCESSCALL, {
        type: DOWNLOADFILE,
        params: url
    })
}
const checkFileExist = (path) => {
    ipcRenderer.send(RENDERPROCESSCALL, {
        type: CHECK_FILE_EXIST,
        params: path
    })
}
const selectFileMessage = () => {
	console.log('8888888')
    ipcRenderer.send(RENDERPROCESSCALL, {
        type: SELECT_FILES,
        params: {
            fileType: "file",
            extensions: ["*"],
            multiSelections: true
        }
    })
}
const selectAudioMessage = () => {
    ipcRenderer.send(RENDERPROCESSCALL, {
        type: SELECT_FILES,
        params: {
            fileType: "file",
            extensions: SUPPORT_AUDIO_TYPE,
            multiSelections: true
        }
    })
}
const selectVideoMessage = () => {
    ipcRenderer.send(RENDERPROCESSCALL, {
        type: SELECT_FILES,
        params: {
            fileType: "video",
            extensions: SUPPORT_VIDEO_TYPE,
            multiSelections: false
        }
    })
}
const selectImageMessage = () => {
    ipcRenderer.send(RENDERPROCESSCALL, {
        type: SELECT_FILES,
        params: {
            fileType: "image",
            extensions: SUPPORT_IMAGE_TYPE,
            multiSelections: false
        }
    })
}
const getVideoInfo = (path) => {
    ipcRenderer.send(RENDERPROCESSCALL, {
        type: GET_VIDEO_INFO,
        params: { path: path }
    });
}
const getVideo = (url)=>{
    console.log("getVideo",url);
    ipcRenderer.send(VIDEO_FFPEG,url);
}
const stopVideo = ()=>{
    console.log("stopVideo");
    ipcRenderer.send(TERMINATE_VIDEO_CONVERSION);
}

const generateRoomID = () => {
    return Math.floor(Math.random() * 1000);
}

const convertAudio = (data)=>{
	console.log('************convertAudio 99')	
	ipcRenderer.send(AUDIO_FFPEG, {
	    type: '',
	    params: {
			inputpath:data.inputpath,
			outpath:data.outpath,
			index:data.index
	    }
	})
}

const selectDir = () => {
    ipcRenderer.send(RENDERPROCESSCALL, {
        type: SELECT_DIR
    });
}
export {
    isWin,
    minSizeWin,
    maxSizeWin,
    closeWin,
    showDialog,
    downloadFilesByUrl,
    checkFileExist,
    generateRoomID,
    selectFileMessage,
    selectVideoMessage,
    selectImageMessage,
	selectAudioMessage,
    getVideoInfo,
    getVideo,
    stopVideo,
    SUPPORT_IMAGE_TYPE,
    SUPPORT_VIDEO_TYPE,
	convertAudio,
	selectDir,
	openPath
}